package com.qipay.mapper;

import com.qipay.baas.model.CpSpChannel;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CpSpChannelMapper{

    @Select({
            "select sp_id as spId " +
                    "from `cp_sp_channel` where cp_id = #{cpId,jdbcType=INTEGER} and channel_type = #{channelType,jdbcType=INTEGER} order by cp_id asc "
    })
    List<Long> selectSpIds(@Param("cpId") int cpId, @Param("channelType") int channelType);

    @Select({
            "select t.sp_id as spId from `cp_sp_channel` t LEFT JOIN sp t1 on t.sp_id=t1.id " +
                    "LEFT JOIN sp_channel t2 on t.sp_id = t2.sp_id " +
                    "where t.cp_id = #{cpId,jdbcType=INTEGER} and t.channel_type = #{channelType,jdbcType=INTEGER} " +
                    "and t1.delete_flag=0 and t1.state=0 " +
                    "and t1.first=#{first,jdbcType=INTEGER} and t2.state=0 and t2.channel_type= #{channelType,jdbcType=INTEGER}"
    })
    List<Long> selectSpIdsFirst(@Param("cpId") int cpId, @Param("channelType") int channelType,@Param("first") int first);

    @Select({
            "select t.sp_id as spId from `cp_sp_channel` t LEFT JOIN sp t1 on t.sp_id=t1.id " +
                    "LEFT JOIN sp_channel t2 on t.sp_id = t2.sp_id " +
                    "where t.cp_id = #{cpId,jdbcType=INTEGER} and t.channel_type = #{channelType,jdbcType=INTEGER} " +
                    "and t1.delete_flag=0 and t1.state=0 and t1.price_support_double=#{priceSupportDouble,jdbcType=INTEGER} " +
                    "and t1.first=#{first,jdbcType=INTEGER} and t2.state=0 and t2.channel_type= #{channelType,jdbcType=INTEGER}"
    })
    List<Long> selectSpIdsFirstDouble(@Param("cpId") int cpId, @Param("channelType") int channelType,
                                @Param("priceSupportDouble") int priceSupportDouble, @Param("first") int first);


    @Select({
            "select id,cp_id as cpId,sp_id as spId,channel_type as channelType ,sp_channel_id as spChannelId " +
                    "from `cp_sp_channel` where sp_channel_id = #{spChannelId,jdbcType=INTEGER} order by cp_id asc "
    })
    List<CpSpChannel> selectBySpChannelId(@Param("spChannelId") int spChannelId);


    @Select({
            "select id,cp_id as cpId,sp_id as spId,channel_type as channelType ,sp_channel_id as spChannelId " +
                    "from `cp_sp_channel` where cp_id = #{cpId,jdbcType=INTEGER} and channel_type = #{channelType,jdbcType=INTEGER} order by cp_id asc "
    })
    List<CpSpChannel> selectByCpIdAndType(@Param("cpId") int cpId, @Param("channelType") int channelType);

    @Delete({
            "delete csc from cp_sp_channel csc inner join cp c on csc.cp_id = c.id  " +
                    "where csc.sp_channel_id = #{spChannelId,jdbcType=INTEGER} and c.agent_id = #{agentId,jdbcType=BIGINT}"
            })
    int deleteBySpChannelId(@Param("spChannelId") Integer spChannelId,@Param("agentId") int agentId);

    @Insert({
            "insert into cp_sp_channel (`cp_id`, sp_id, ",
            "channel_type, sp_channel_id )",
            "values (#{cpId,jdbcType=INTEGER}, #{spId,jdbcType=INTEGER}, ",
            "#{channelType,jdbcType=INTEGER}, #{spChannelId,jdbcType=INTEGER} )"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(CpSpChannel record);

}